layui.use('table', function () {
    var table = layui.table;
    //第一个实例
    table.render({
        elem: '#teacher-list'
        , height: 'full'
        , url: '/teacher/list' //数据接口
        , parseData: function (res) {
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": switchGender(res.data.data) //解析数据列表
            }
        }
        , request: {
            method: 'GET'
            , pageName: 'page' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        }
        , toolbar: 'default' //开启头部工具栏，并为其绑定左侧模板
        , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '提示'
            , layEvent: 'LAYTABLE_TIPS'
            , icon: 'layui-icon-tips'
        }]
        , title: '用户数据表'
        , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', title: 'ID', width: 80}
            , {field: 'teacherId', title: '教师编号', width: 150}
            , {field: 'name', title: '教师名', width: 150}
            , {field: 'gender', title: '性别', width: 80}
            , {field: 'email', title: '邮箱', width: 250}
            , {field: 'phone', title: '电话', width: 200}
            , {field: 'collegeId', title: '学院', width: 100}
            , {field: 'systemId', title: '系', width: 100}
            , {field: 'role', title: '角色', width: 80}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo', width: 200, align: 'center'}
        ]]
        // 开启分页
        , page: true
    });

    function switchGender(teacherArr) {
        for (let teacher of teacherArr) {
            if (teacher.gender === 0) {
                teacher.gender = '男';
            } else {
                teacher.gender = '女';
            }
        }
        return teacherArr;
    }

    //监听头工具栏事件
    table.on('toolbar(teacher-list)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; // 获取选中的数据
        switch (obj.event) {
            case 'add':
                // layer.msg('添加');
                $("#body-content").load("/pages/teacher/teacherAdd");
                $("#pages\\/teacherList").parent().attr('class', '');
                $("#pages\\/teacherAdd").parent().attr('class', 'layui-this');
                break;
            case 'update':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else if (data.length > 1) {
                    layer.msg('只能同时编辑一个');
                } else {
                    // layer.alert('编辑 [id]：' + checkStatus.data[0].id);
                    updateTeacher(checkStatus.data[0].id);
                }
                break;
            case 'delete':
                if (data.length === 0) {
                    layer.msg('请选择一行');
                } else {
                    // layer.msg('删除');
                    layer.confirm('真的删除这些行么', function (index) {
                        console.log(data);
                        for (let teacher of data) {
                            if (deleteTeacher(teacher.id) !== 0) {
                                break;
                            }
                        }
                        // layer.close(index);
                        // deleteTeacher();
                    });
                }
                break;
        }
    });

    //监听行工具事件
    table.on('tool(teacher-list)', function (obj) {
        const data = obj.data;
        // console.log(obj)
        if (obj.event === 'del') {
            layer.confirm('真的删除行么', function (index) {
                // layer.msg(obj.data.id);
                layer.close(index);
                deleteTeacher(obj.data.id);
            });
        } else if (obj.event === 'edit') {
            updateTeacher(obj.data.id);
        }
    });


    function updateTeacher(id) {
        window.sessionStorage.setItem('teacherID', id);
        layer.open({
            type: 2,
            title: '老师信息栏',
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['800px', '520px'],
            content: '/pages/teacher/teacherUpdate',
            cancel: function (index) {
                layer.close(index);
            }
        });
    }

    function deleteTeacher(id) {
        let code = 0;
        $.ajax({
            url: 'teacher/delete',
            type: 'GET',
            data: {id: id},
            success: function (res) {
                if (res.code === 0) {
                    layer.msg("删除" + id + "成功！");
                } else {
                    layer.msg("删除" + id + "失败！");
                }
                code = res.code;
            }
        })
        return code;
    }
});

